/****************************************************************************
**
** GameBlockedGump.cpp
**
** Copyright (C) August 2014 Hotride
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
*****************************************************************************
*/
//---------------------------------------------------------------------------
#include "stdafx.h"
//---------------------------------------------------------------------------
TGameBlockedGump::TGameBlockedGump()
: m_Code(0)
{
}
//---------------------------------------------------------------------------
TGameBlockedGump::~TGameBlockedGump()
{
}
//---------------------------------------------------------------------------
void TGameBlockedGump::Init()
{
	m_Code = 0;
}
//---------------------------------------------------------------------------
void TGameBlockedGump::SetState(BYTE code)
{
	m_Code = code;
}
//---------------------------------------------------------------------------
int TGameBlockedGump::Render(bool mode)
{
	int OffsX = 0;
	int OffsY = 0;

	if (g_LeftMouseDown && g_LastObjectLeftMouseDown == 1)
	{
		OffsX = g_MouseX - g_DroppedLeftMouseX;
		OffsY = g_MouseY - g_DroppedLeftMouseY;
	}
	
	if (mode)
	{
		TGump *gump = UO->m_GumpList;

		while (gump != NULL)
		{
			TGump *next = gump->m_Next;

			if (gump->GetType() == GT_GRAY_MENU) gump->Draw(mode);
			else if (gump->GetType() == GT_TEXT_ENTRY_DIALOG) gump->Draw(mode);
			else if (gump->GetType() == GT_QUESTION) gump->Draw(mode);
			else if (gump->GetType() == GT_NOTIFY && gump->GetPage()) gump->Draw(mode);

			gump = next;
		}
		
		UO->DrawCursor(0x2073); //Main Gump mouse cursor
	}
	else
	{
		g_LastSelectedObject = 0;
		g_LastObjectType = SOT_NO_OBJECT;
		g_LastSelectedGump = 0;

		TGump *gump = UO->m_GumpList;

		while (gump != NULL)
		{
			TGump *next = gump->m_Next;

			int idx = 0;

			if (gump->GetType() == GT_GRAY_MENU || gump->GetType() == GT_TEXT_ENTRY_DIALOG || gump->GetType() == GT_QUESTION || (gump->GetType() == GT_NOTIFY && gump->GetPage())) idx = gump->Draw(mode);

			if (idx) g_LastSelectedGump = (DWORD)gump;

			gump = next;
		}

		return g_LastSelectedObject;
	}
	return 0;
}
//---------------------------------------------------------------------------
void TGameBlockedGump::ParseLeftMouseDown()
{
	g_LastSelectedGump = 0;

	Render(false);

	if (g_LastSelectedGump)
	{
		TGump *gump = UO->m_GumpList;

		while (gump != NULL)
		{
			if (g_LastSelectedGump == (DWORD)gump)
			{
				UO->MoveGumpToTop(gump);
				g_LastSelectedGump = (DWORD)gump;

				break;
			}

			gump = gump->m_Next;
		}
	}
}
//---------------------------------------------------------------------------
void TGameBlockedGump::ParseLeftMouseUp()
{
	Render(false);

	if (g_LeftMouseDown && g_LastGumpLeftMouseDown)
	{
		TGump *gump = UO->m_GumpList;

		while (gump != NULL)
		{
			if (g_LastGumpLeftMouseDown == (DWORD)gump)
			{
				if (!g_LastObjectLeftMouseDown && (gump->GetType() == GT_QUESTION || (gump->GetType() == GT_NOTIFY && gump->GetPage())))
				{
					int GX = gump->GetX() + (g_MouseX - g_DroppedLeftMouseX);
					int GY = gump->GetY() + (g_MouseY - g_DroppedLeftMouseY);
					gump->SetX(GX);
					gump->SetY(GY);
				}

				gump->OnButtonUp();
				break;
			}

			gump = gump->m_Next;
		}
	}
}
//---------------------------------------------------------------------------
void TGameBlockedGump::ParseRightMouseDown()
{
}
//---------------------------------------------------------------------------
void TGameBlockedGump::ParseRightMouseUp()
{
}
//---------------------------------------------------------------------------
bool TGameBlockedGump::ParseLeftMouseDoubleClick()
{
	return false;
}
//---------------------------------------------------------------------------
bool TGameBlockedGump::ParseRightMouseDoubleClick()
{
	return false;
}
//---------------------------------------------------------------------------
void TGameBlockedGump::ParseMouseWheel(MOUSE_WHEEL_STATE state)
{
}
//---------------------------------------------------------------------------
void TGameBlockedGump::ParseCharPress(WPARAM wparam, LPARAM lparam)
{
	if (g_EntryPointer == NULL || g_EntryPointer == UO->m_GameConsole) return;

	TGump *gump = UO->m_GumpList;

	while (gump != NULL)
	{
		if (gump->GetType() == GT_TEXT_ENTRY_DIALOG)
		{
			if (gump->GetTextEntry() == g_EntryPointer)
			{
				gump->OnCharPress(gump, wparam, lparam);
				break;
			}
		}
		gump = gump->m_Next;
	}
}
//---------------------------------------------------------------------------
void TGameBlockedGump::ParseKeyPress(WPARAM wparam, LPARAM lparam)
{
	if (g_EntryPointer == NULL || g_EntryPointer == UO->m_GameConsole)
	{
		if (wparam == VK_RETURN)
		{
			TGump *gump = UO->m_GumpList;

			while (gump != NULL)
			{
				if (gump->GetType() == GT_NOTIFY)
				{
					gump->OnKeyPress(gump, wparam, lparam);
					break;
				}
				gump = gump->m_Next;
			}
		}
		return;
	}

	TGump *gump = UO->m_GumpList;

	while (gump != NULL)
	{
		if (gump->GetType() == GT_TEXT_ENTRY_DIALOG)
		{
			if (gump->GetTextEntry() == g_EntryPointer)
			{
				gump->OnKeyPress(gump, wparam, lparam);
				break;
			}
		}
		else if (gump->GetType() == GT_NOTIFY && gump->GetPage())
		{
			gump->OnKeyPress(gump, wparam, lparam);
			break;
		}
		gump = gump->m_Next;
	}
}
//---------------------------------------------------------------------------